package spride;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cc
 * @date 2018年1月5日15:26:48
 */
public class SprideAllUrl {
    private static Logger logger = Logger.getLogger(SprideAllUrl.class);
    private static int size = 20;
    public static String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36";

    public static void deal(int index) {
        try {//2-21 22-41 22-31
            logger.debug("start get page " + (2 + index * size) + "--" + (1 + (index + 1) * size));
            List<VideoEntity> list = new ArrayList<>();
            for (int i = (2 + index * size); i < (2 + (index + 1) * size); i++) {
                String url = "http://www.yjizz8.com/recent/" + i + "/";
                Document doc = Jsoup.connect(url).userAgent(userAgent).get();
                Elements es = doc.select("div.video");
                for (Element e : es) {
                    String sourceUrl = e.select("a").get(0).attr("href");
                    String title = e.select("a").get(0).attr("title");
                    String imageUrl = e.select("img ").attr("src");
                    VideoEntity videoEntity = new VideoEntity();
                    videoEntity.setImageUrl(imageUrl);
                    videoEntity.setTitle(title);
                    videoEntity.setSourceUrl(sourceUrl);
                    list.add(videoEntity);
                }
            }
            try {
                batchInsert(list);
                logger.debug("saved page: " + (2 + index * size) + "--" + (1 + (index + 1) * size));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String url = "jdbc:mysql://127.0.0.1:3306/video?useServerPrepStmts=false&rewriteBatchedStatements=true";
    public static String user = "root";
    public static String password = "root";

    public static Connection getConnect() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void batchInsert(List<VideoEntity> list) throws ClassNotFoundException, SQLException {
        long start = System.currentTimeMillis();
        Connection connection = getConnect();
        connection.setAutoCommit(false);
        PreparedStatement cmd = connection
                .prepareStatement("INSERT INTO video VALUES(?,?,?,?)");

        for (VideoEntity videoEntity : list) {
            cmd.setInt(1, 0);
            cmd.setString(2, videoEntity.getTitle());
            cmd.setString(3, videoEntity.getSourceUrl());
            cmd.setString(4, videoEntity.getImageUrl());
            cmd.addBatch();
        }
        cmd.executeBatch();
        connection.commit();
        cmd.close();
        connection.close();
    }


    public static void main(String[] args) throws Exception {
      /* 测试 第2页
      String url="http://www.yjizz8.com/recent/"+2+"/";
        Document doc=  Jsoup.connect(url).userAgent(userAgent).get();
        Elements es= doc.select("div.video");
        List list=new ArrayList();
        for (Element e:es) {
            String sourceUrl=e.select("a").get(0).attr("href");
            String title=e.select("a").get(0).attr("title");
            String imageUrl=e.select("img ").attr("src");
            VideoEntity videoEntity=new VideoEntity();
            videoEntity.setImageUrl(imageUrl);
            videoEntity.setTitle(title);
            videoEntity.setSourceUrl(sourceUrl);
            list.add(videoEntity);
        }
        try {
            batchInsert(list);
            logger.debug("saved: page 2");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(0);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(1);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(2);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(3);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(4);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(5);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(6);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                deal(7);
            }
        }).start();
    }

}
